package com.ymatou.iapi.trading.useranalysis.testcase;

import java.io.IOException;
import java.math.BigDecimal;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.useranalysis.parameter.ChangeOrderCountSummaryReq;
import com.ymatou.iapi.trading.useranalysis.service.ChangeOrderCountSummaryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.mongo.OrderCountSummaryBean;
import com.ymttest.database.mongo.UserAnalysisMongoWapper;
import com.ymttest.utils.EnvSetup;
/**
*更新订单汇总数量接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ChangeOrderCountSummary {
	private static ChangeOrderCountSummaryReq bean;
	private static ChangeOrderCountSummaryCall call;

	private static UserAnalysisMongoWapper buyerSellerOrderStatistics = new UserAnalysisMongoWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新订单汇总数量接口");
	}

	@Before
	public void caseUp() {
		bean = new ChangeOrderCountSummaryReq();
		call = new ChangeOrderCountSummaryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 
	 * 新用户下单
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * 
	 */
	public static PlaceOrderResp placeOrder() throws Exception {

		int userId = new OrderCallService().createNewUser();

		PlaceOrderReq placeorderBean = PlaceOrderService
				.getDefaultOrderReq(userId);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}

	/**
	 * 
	 * 新用户一次下多单
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * 
	 */
	public static PlaceOrderResp placeOrder2() throws Exception {

		int userId = new OrderCallService().createNewUser();

		PlaceOrderReq placeorderBean = PlaceOrderService
				.getDefaultOrderReq(userId);

		placeorderBean.getOrderGroups().add(
				PlaceOrderService.getDefaultOrderGroupVo(userId,
						Integer.valueOf(EnvSetup.getData("SellerId2"))));

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}

	public static long callChangeOrderCountSummary() {

		int userId = 0;

		try {

			bean = new ChangeOrderCountSummaryReq();
			call = new ChangeOrderCountSummaryCall();

			PlaceOrderResp placeOrderResp = placeOrder();

			int tradingId = placeOrderResp.getTradingId();

			userId = placeOrderResp.getUserId();

			BigDecimal price = placeOrderResp.getPayAmount();

			new OrderCallService().orderPayRequest(userId, price, tradingId);

			bean.setUserId(userId);

			call.setData(false, bean);

			call.callService();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return userId;

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeOrderCountSummary_001() {
		Logger.start(true, "一笔交易一个订单，支付后");
		try {

			PlaceOrderResp placeOrderResp = placeOrder();

			int tradingId = placeOrderResp.getTradingId();
			int userId = placeOrderResp.getUserId();

			BigDecimal price = placeOrderResp.getPayAmount();

			new OrderCallService().orderPayRequest(userId, price, tradingId);

			bean.setUserId(userId);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "校验接口返回");

			Logger.comment("校验userId为:" + userId);

			OrderCountSummaryBean orderCountSummary = buyerSellerOrderStatistics
					.findOrderCountSummaryById(userId);

			Logger.verifyEquals(Long.valueOf(userId),
					orderCountSummary.getId(), "验证id");

			Logger.verifyEquals(1L, orderCountSummary.getPayCount(),
					"验证PayCount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeOrderCountSummary_002() {
		Logger.start(true, "一笔交易多个订单，支付后");
		try {

			PlaceOrderResp placeOrderResp = placeOrder2();

			int tradingId = placeOrderResp.getTradingId();
			int userId = placeOrderResp.getUserId();

			BigDecimal price = placeOrderResp.getPayAmount();

			new OrderCallService().orderPayRequest(userId, price, tradingId);

			bean.setUserId(userId);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "校验接口返回");

			Logger.comment("校验userId为:" + userId);

			OrderCountSummaryBean orderCountSummary = buyerSellerOrderStatistics
					.findOrderCountSummaryById(userId);

			Logger.verifyEquals(Long.valueOf(userId),
					orderCountSummary.getId(), "验证id");

			Logger.verifyEquals(1L, orderCountSummary.getPayCount(),
					"验证PayCount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeOrderCountSummary_003() {
		Logger.start(true, "用户支付笔数超过3次后，支付后");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.payOrder();

			int userId = placeOrderResp.getUserId();

			bean.setUserId(userId);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getReturnData(), "校验接口返回");

			Logger.comment("校验userId为:" + userId);

			OrderCountSummaryBean orderCountSummary = buyerSellerOrderStatistics
					.findOrderCountSummaryById(userId);

			Logger.verifyEquals(Long.valueOf(userId),
					orderCountSummary.getId(), "验证id");

			Logger.verifyEquals(68746L, orderCountSummary.getPayCount(),
					"验证PayCount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeOrderCountSummary_004() {
		Logger.start(true, "userid为null");
		try {

			//bean.setUserId(userId);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("fail", call.getReturnData(), "校验接口返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}